package cn.bob.product.controller;

import cn.bob.basic.jwt.LoginContext;
import cn.bob.org.domain.Employee;
import cn.bob.pet.dto.HandleDto;
import cn.bob.org.mapper.EmployeeMapper;
import cn.bob.product.dto.ProductDto;
import cn.bob.product.service.IProductStaycheckService;
import cn.bob.product.domain.ProductStaycheck;
import cn.bob.product.query.ProductStaycheckQuery;
import cn.bob.basic.utils.AjaxResult;
import cn.bob.basic.utils.PageList;
import cn.bob.user.domain.Logininfo;
import cn.bob.user.domain.User;
import cn.bob.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/productStaycheck")
public class ProductStaycheckController {
    @Autowired
    private IProductStaycheckService productStaycheckService;

    @Autowired
    private IUserService userService;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 保存和修改公用的
     * @param productStaycheck  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ProductStaycheck productStaycheck){
        try {
            if( productStaycheck.getId()!=null)
                productStaycheckService.update(productStaycheck);
            else
                productStaycheckService.add(productStaycheck);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productStaycheckService.delById(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public ProductStaycheck get(@PathVariable("id")Long id)
    {
        return productStaycheckService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<ProductStaycheck> list(){

        return productStaycheckService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<ProductStaycheck> json(@RequestBody ProductStaycheckQuery query)
    {
        return productStaycheckService.queryByPage(query);
    }

    /**
     *
     * 前端传入的id生成待处理消息
     * @param
     * @return
     */
    @GetMapping("/producttoHandle/staycheck/{id}")
    public AjaxResult staycheck(@PathVariable("id") Long id, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userService.findLoginid(logininfo.getId());
        productStaycheckService.staycheck(id,user.getId());
        return AjaxResult.me().success();
    }

    /**
     * 接单 生成订单消息
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody ProductDto productDto,HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee emp = employeeMapper.findByLogid(logininfo.getId());
        productStaycheckService.accept(productDto,emp.getId());
        return AjaxResult.me().success();
    }

    /**
     * todo 拒绝订单生成拒单日志
     * 修改状态为-1
     * @return
     */
    @PostMapping("/reject/{stayId}")
    public AjaxResult reject(@PathVariable("stayId") Long stayId,HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee emp = employeeMapper.findByLogid(logininfo.getId());
        productStaycheckService.reject(stayId,emp.getId());
        return AjaxResult.me().success();
    }
}
